本文同步發表於小弟自架網站:微確幸資訊站
以下程式碼源自於ChatGPT,主要變數說明如下:
n_students:學生人數
n_exams:考試科目數
mean:考試成績平均數
std_dev:考試成績標準差
import pandas as pd
import numpy as np
# Set the number of students and the number of exams
n_students = 20
n_exams = 5
# Set the mean and standard deviation for the normal distribution
mean = 70
std_dev = 10
# Generate the random scores using numpy
scores = np.random.normal(loc=mean, scale=std_dev, size=(n_students, n_exams))
# Create a pandas DataFrame to store the scores
df = pd.DataFrame(scores)
# Set the column names
df.columns = ['Exam ' + str(i+1) for i in range(n_exams)]
# Set the row names as the student IDs
df.index = ['Student ' + str(i+1) for i in range(n_students)]
# Print the DataFrame
print(df)